package com.whnk.web.controller.pm;

import java.text.ParseException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.enums.BusinessType;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.pm.domain.SignInfo;
import com.whnk.pm.dto.SignSyncDTO;
import com.whnk.pm.service.ISignInfoService;

/**
 * 项目考勤 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/pm/signInfo")
public class SignInfoController extends BaseController
{
    private String prefix = "pm/signInfo";
	
	@Autowired
	private ISignInfoService signInfoService;
	
	@RequiresPermissions("pm:signInfo:view")
	@GetMapping()
	public String signInfo()
	{
	    return prefix + "/signInfo";
	}
	
	/**
	 * 查询项目考勤列表
	 */
	@RequiresPermissions("pm:signInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SignInfo signInfo)
	{
		startPage();
        List<SignInfo> list = signInfoService.selectSignInfoList(signInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增项目考勤
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目考勤
	 */
	@RequiresPermissions("pm:signInfo:add")
	@Log(title = "项目考勤", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(SignInfo signInfo)
	{		
		return toAjax(signInfoService.insertSignInfo(signInfo));
	}

	/**
	 * 修改项目考勤
	 */
	@GetMapping("/edit/{signId}")
	public String edit(@PathVariable("signId") Integer signId, ModelMap mmap)
	{
		SignInfo signInfo = signInfoService.selectSignInfoById(signId);
		mmap.put("signInfo", signInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目考勤
	 */
	@RequiresPermissions("pm:signInfo:edit")
	@Log(title = "项目考勤", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SignInfo signInfo)
	{		
		return toAjax(signInfoService.updateSignInfo(signInfo));
	}
	
	/**
	 * 删除项目考勤
	 */
	@RequiresPermissions("pm:signInfo:remove")
	@Log(title = "项目考勤", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(signInfoService.deleteSignInfoByIds(ids));
	}

	@PostMapping(value = "/syncSignInfo")
	@ResponseBody()
	public AjaxResult syncSignInfo(@RequestBody SignSyncDTO signSyncDTO) {
		signInfoService.syncSignInfo(signSyncDTO);
		return success();
	}
	
	/**
	 * 查看详细信息
	 * 
	 * @param signId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("pm:signInfo:detail")
    @GetMapping("/detail/{signId}")
    public String detail(@PathVariable("signId") Integer signId, ModelMap mmap)
	{
		SignInfo signInfo = signInfoService.selectSignInfoById(signId);
		mmap.put("signInfo", signInfo);
        return prefix + "/detail";
    }

	@GetMapping("/manualSign")
	public String manualSignHtml() {
		return prefix + "/manualSign";
	}

	@PostMapping("/manualSign")
	@ResponseBody
	public AjaxResult manualSign(String empNm, String proName) throws ParseException {
		String empId = empNm.split("-")[1];
		signInfoService.manualSign(empId, proName);
		return success();
	}

	@PostMapping("/manualSignOut")
	@ResponseBody
	public AjaxResult manualSignOut(String empNm, String proName) throws ParseException {
		String empId = empNm.split("-")[1];
		signInfoService.manualSignOut(empId, proName);
		return success();
	}
}
